package com.wcl.demo.ui.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.ViewDragHelper;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMConnectionListener;
import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.util.NetUtils;
import com.wcl.demo.R;
import com.wcl.demo.ui.fragment.FindFragment;
import com.wcl.demo.ui.fragment.MineFragment;
import com.wcl.demo.ui.fragment.NewsFragment;
import com.wcl.demo.ui.fragment.PersonFragment;

import com.nineoldandroids.view.ViewHelper;

import com.wcl.demo.utils.ActivityUtils;
import com.wcl.demo.utils.AppHelper;
import com.wcl.demo.utils.DrawerEdgeSizeUtils;
import com.wcl.demo.utils.FaceConversionUtil;
import com.wcl.demo.view.CustomToast;

import java.lang.reflect.Field;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {


    private TabHost th;
    private TabHost.TabSpec news_spec, person_spec, find_spec, mine_spec;
    private View news_view, person_view, find_view, mine_view;
    private FragmentManager fm;
    private String loginName;
    private int tab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = ActivityUtils.fromSrc(this);
       if(bundle!=null){
          this.tab=bundle.getInt("tab");
       }
        initView();
        showTabHost();
        initFragment();
        th.setCurrentTab(tab);
      //  initEvents();

        //注册一个监听连接状态的listener
        EMChatManager.getInstance().addConnectionListener(new EMConnectionListener() {
            @Override
            public void onConnected() {
                Log.i("lanou333", "已连接服务器");
            }

            @Override
            public void onDisconnected(int error) {
                if (error == EMError.USER_REMOVED) {
                    // 显示帐号已经被移除
                } else if (error == EMError.CONNECTION_CONFLICT) {
                    CustomToast.toastShow(MainActivity.this , "账号在其他设备上登录");
                    AppHelper.getInstance().logout(false , null);
                    showAlertDialog();
                    Log.i("lanou333" , "账号在其他设备登陆");
                } else {
                    if (NetUtils.hasNetwork(MainActivity.this)){
                        //连接不到聊天服务器
                        CustomToast.toastShow(MainActivity.this, "连接=不到服务器");
                    }else {
                        //当前网络不可用，请检查网络设置
                        CustomToast.toastShow(MainActivity.this , "当前网络不可用，请检查网络设置");
                    }
                }
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                FaceConversionUtil.getInstace().getFileText(getApplication());
            }
        }).start();
    }

//    public void initEvents() {
//        DrawerEdgeSizeUtils.setDrawerLeftEdgeSize(this, main_drawerLayout, 0.3f);
//        DrawerEdgeSizeUtils.setDrawerRightEdgeSize(this, main_drawerLayout, 0.2f);
//        main_drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
//
//            @Override
//            public void onDrawerSlide(View drawerView, float slideOffset) {
//                View mContent = main_drawerLayout.getChildAt(0);
//                View mMenu = drawerView;
//                float scale = 1 - slideOffset;
//                float rightScale = 0.8f + scale * 0.2f;
//
//                if (drawerView.getTag().equals("LEFT")) {
//                    float leftScale = 1 - 0.3f * scale;
//                    ViewHelper.setScaleX(mMenu, leftScale);
//                    ViewHelper.setScaleY(mMenu, leftScale);
//                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
//                    ViewHelper.setTranslationX(mContent,
//                            mMenu.getMeasuredWidth() * (1 - scale));
//                    ViewHelper.setPivotX(mContent, 0);
//                    ViewHelper.setPivotY(mContent,
//                            mContent.getMeasuredHeight() / 2);
//                    mContent.invalidate();
//                    ViewHelper.setScaleX(mContent, rightScale);
//                    ViewHelper.setScaleY(mContent, rightScale);
//
//
//                } else if (drawerView.getTag().equals("RIGHT")) {
//
//                    ViewHelper.setTranslationX(mContent,
//                            -mMenu.getMeasuredWidth() * slideOffset);
//                    ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());
//                    ViewHelper.setPivotY(mContent,
//                            mContent.getMeasuredHeight() / 2);
//                    mContent.invalidate();
//                    ViewHelper.setScaleX(mContent, rightScale);
//                    ViewHelper.setScaleY(mContent, rightScale);
////                    float leftScale = 1 - 0.3f * scale;
////
////                    ViewHelper.setScaleX(mMenu, leftScale);
////                    ViewHelper.setScaleY(mMenu, leftScale);
////                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
////                    ViewHelper.setTranslationX(mContent,
////                            -mMenu.getMeasuredWidth() * (1 - scale));
////                    ViewHelper.setPivotX(mContent, 0);
////                    ViewHelper.setPivotY(mContent,
////                            mContent.getMeasuredHeight() / 2);
////                    mContent.invalidate();
////                    ViewHelper.setScaleX(mContent, rightScale);
////                    ViewHelper.setScaleY(mContent, rightScale);
//                }
//            }
//
//
//            @Override
//            public void onDrawerOpened(View drawerView) {
////                main_drawerLayout.setDrawerLockMode(
////                        DrawerLayout.LOCK_MODE_UNLOCKED,Gravity.RIGHT);
//            }
//
//            @Override
//            public void onDrawerClosed(View drawerView) {
//
//            }
//
//            @Override
//            public void onDrawerStateChanged(int newState) {
//
//            }
//        });
//    }


    @Override
    protected void onStart() {
        super.onStart();

    }

    protected void initView() {
        fm = getSupportFragmentManager();
        th = (TabHost) findViewById(android.R.id.tabhost);
        th.setup();
    }

    private void showTabHost() {
        setTabHost(getTabSpec(news_spec, "news"),
                getPageView(news_view, R.layout.tab_news_indicator),
                R.id.newsView);
        setTabHost(getTabSpec(person_spec, "person"),
                getPageView(person_view, R.layout.tab_person_indicator),
                R.id.personView);
        setTabHost(getTabSpec(mine_spec, "find"),
                getPageView(find_view, R.layout.tab_find_indicator),
                R.id.findView);
        setTabHost(getTabSpec(mine_spec, "mine"),
                getPageView(mine_view, R.layout.tab_mine_indicator),
                R.id.mineView);
    }

    private TabHost.TabSpec getTabSpec(TabHost.TabSpec spec, String text) {
        spec = th.newTabSpec(text);
        return spec;
    }

    private View getPageView(View view, int anchor) {
        view = getLayoutInflater().inflate(anchor, null);
        return view;
    }

    private void setTabHost(TabHost.TabSpec spec, View view, int anchor) {
        spec.setIndicator(view);
        spec.setContent(anchor);
        th.addTab(spec);
    }

    private void initFragment() {
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.newsView, new NewsFragment());
        ft.replace(R.id.personView, new PersonFragment());
        ft.replace(R.id.mineView, new MineFragment());
        ft.replace(R.id.findView, new FindFragment());
        ft.commit();
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }


    @Override
    protected void initData() {
    }

    /***
     * 弹出一个AlertDialog对话框
     */
    private void showAlertDialog() {
        // TODO Auto-generated method stub
        // 用内部类进行,类名.内部类
        // 创建一个AlertDialog.Builder对象
        // 因为activity继承与context所以可以用this
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("下线通知");// 标题文字
        builder.setIcon(R.mipmap.bg_news);// 标题边上的图标

        // builder.setMessage(R.string.dialogmessage);// 设置信息内容

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "当前点击的是 确定 按钮",
                        Toast.LENGTH_LONG).show();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "当前点击的是  取消 按钮",
                        Toast.LENGTH_LONG).show();
            }
        });

        builder.show();// 显示弹出提示框
    }

}
